package io.fredia.femicro.graph.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.fredia.femicro.graph.domain.CallRelation;
import io.fredia.femicro.graph.domain.Mobile;
import io.fredia.femicro.graph.domain.OwnRelation;
import io.fredia.femicro.graph.domain.User;
import io.fredia.femicro.graph.repository.MobileRepository;
import io.fredia.femicro.graph.repository.UserRepository;
import io.fredia.femicro.graph.utils.ChineseName;

@Service
public class UserService {

	private final static Logger LOG = LoggerFactory.getLogger(UserService.class);
	@Autowired
	private MobileRepository mobileRepository;
	@Autowired
	private UserRepository userRepository;

	public void initData() {
		int count = 0;
		do {
			init();
			count++;
		} while (count < 500);

	}

	/**
	 * 取随机手机号
	 * 
	 * @return
	 * @author : Fredia
	 * @since : 2018年5月29日
	 * @return :String
	 */
	private String randomMobile() {
		String[] mobiles = new String[] { "1329876541", "1329876542", "1329876543", "1329876544", "1329876545",
				"1329876546", "1329876547", "1329876548", "1329876549", "1329876540" };
		int index = (int) (Math.random() * mobiles.length);
		return mobiles[index];
	}

	public void init() {
		User user = new User(ChineseName.getChinese());

		userRepository.save(user);

		Mobile mobile = new Mobile(randomMobile());

		mobileRepository.save(mobile);

		OwnRelation own = new OwnRelation(user, mobile);
		own.addOwnRelationName("Own");

		User user2 = new User(ChineseName.getChinese());

		CallRelation call = new CallRelation(user2, mobile);
		call.addCallRelationName("Call");

		mobile.addOwnRelations(own);
		mobile.addCallRelations(call);

		mobileRepository.save(mobile);

	}
}
